package ui.controls;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import ui.components.ImageControl;
import ui.Assets;
import ui.control.button.*;
import ui.control.binary.States;

public class Button extends ImageControl implements ui.control.binary.Control {

	public States state;
	protected Image image;
	private ButtonBank owner;
	private int id;
	public ButtonTypes type;
	
	public Button( Assets assets, ButtonBank owner, int id, ButtonColors color, ButtonSizes size, ButtonTypes type ) {
		this.owner = owner;
		this.id = id;
		this.type = type;
		
		addMouseListener(
			new MouseAdapter( ) {
				public void mousePressed( MouseEvent e ) {
					mouseDown( e );
					repaint();
				}
				public void mouseReleased( MouseEvent e ) {
					mouseUp( e );
					repaint();
				}
			}
		);
		
		if( color == ButtonColors.RED )
			imagelist = assets.buttonRed24;
		else if( color == ButtonColors.BLUE )
			imagelist = assets.buttonBlue24;
		else if( color == ButtonColors.GREEN )
			imagelist = assets.buttonGreen24;
		else if( color == ButtonColors.YELLOW )
			imagelist = assets.buttonYellow24;
		
		width = 30;
		height = 24;

		setSize( width, height);
		state = States.OFF;
		currentImage = 0;
		image = imagelist.image( currentImage );
	}
	public void setState( States state ) {
		switch( state ) {
		case OFF:
			currentImage = 0;
			image = imagelist.image( currentImage );
			this.state = States.OFF;
			repaint();
			break;
		case ON:
			currentImage = 1;
			image = imagelist.image( currentImage );
			this.state = States.ON;
			repaint();
			break;
		}
	}
	public States getState( ) {
		return state;
	}
	public States toggle( ) {
		switch( state ) {
		case OFF:
			setState( States.ON );
			break;
		case ON:
			setState( States.OFF );
			break;
		}
		return state;
	}
	public void toggleOn( ) {
		setState( States.ON );
	}
	public void toggleOff( ) {
		setState( States.OFF );
	}
	@Override public void mouseDown( MouseEvent e ) {
		switch( type ) {
		case SPRING:
			toggleOn( );
			owner.changed( id, state );
			break;
		case TOGGLE:
			toggle( );
			owner.changed( id, state );
			break;
		}
	}
	@Override public void mouseUp( MouseEvent e ) {
		switch( type ) {
		case SPRING:
			toggleOff( );
			owner.changed( id, state );
			break;
		case TOGGLE:
			break;
		}
	}
}
